/**
 * MIT License
 *
 * Copyright (C) 2024 Huawei Device Co., Ltd.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

import fs from '@ohos.file.fs';
import util from '@ohos.util';
//import { DATA_ENCODE_URI } from './ReactNativeBlobUtilConst';

export default class FileUtils {
  private DATA_ENCODE_URI;


  async copyFileToCache(cacheDir: string, uri: string):Promise<String> {
    let id = uri.split('/').pop();
    let imagePath = `${cacheDir}/${id}`;
    if(fs.accessSync(imagePath)) {
      return imagePath;
    }
    let file = await fs.open(uri);
    try {
      fs.copyFile(file.fd,imagePath);
    } catch (err) {
      console.info(`copyFileToCache copyFileSync err = ${err}`)
    }
    await fs.close(file.fd);
    return imagePath;
  }

  createFile(path:string,data:string,encoding:string) {
    //同步形式检查文件是否存在
    let created = fs.accessSync(path)
    if(encoding === this.DATA_ENCODE_URI) {

    } else {
      if(created) {
        return;
      }
      let file = fs.openSync(path, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
      fs.writeSync(file.fd,data)
    }
  }


}